<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/27
 * Time: 15:11
 */

require_once 'db.php';
require_once 'response.php';

header('content-text:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$id = isset($_POST['teamId']) ? $_POST['teamId'] : '';
//$id = isset($_GET['teamId']) ? $_GET['teamId'] : '';
$status = isset($_POST['showStatus']) ? $_POST['showStatus'] : '';
//$status = 1;

if(!is_numeric($id) || !is_numeric($status)) {
    return Response::show(401,'数据不合法');
}

try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "SELECT team_show FROM team WHERE team_id={$id}";

$res = $connect->query($sql);
$data = array(
    'nowStatus' => $status
);

if($res && $res->num_rows>0) {
    $show = $res->fetch_row();
    $res->free_result();
    if($show[0] == 1) {
        $now = 0;
    } else {
        $now = 1;
    }
    $sql = "UPDATE team SET team_show={$now} WHERE team_id={$id}";
    $res = $connect->query($sql);
    if($res) {
        $data['nowStatus'] = $now;
        return Response::show(200,'团队首页团队展示状态改变成功',$data);
    } else {
        return Response::show(402,'状态改变失败，团队首页展示状态不改变',$data);
    }

} else {
    return Response::show(404,'找不到此id的团队信息',$data);
}
